package com.tzxs.pay.main.bill;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.tzxs.pay.R;
import com.tzxs.pay.main.deposit.TypeNavigator;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = TimeFragment.class.getSimpleName();
    private static TimeFragment timeFragment;
    //这个fragment传值的方法在哪里  我之前写的接口啊
    private TextView fastAll;
    private TextView fastToady;
    private TextView fastYes;
    private TextView fastThisWeek;
    private TextView fastThisMonth;
    private TextView fastLastMonth;
    //选择日期
    private TextView startTime;
    private TextView endTime;
    private String startTimeStr;//开始时间
    private String endTimeStr;//结束时间


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setNavigator((TypeNavigator) context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.pop_order_time, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fastAll = view.findViewById(R.id.fast_choose_all);
        fastToady = view.findViewById(R.id.fast_choose_today);
        fastYes = view.findViewById(R.id.fast_choose_yesterday);
        fastThisWeek = view.findViewById(R.id.fast_choose_this_week);
        fastThisMonth = view.findViewById(R.id.fast_choose_this_month);
        fastLastMonth = view.findViewById(R.id.fast_choose_last_month);
        fastAll.setOnClickListener(this);
        fastAll.setSelected(true);
        fastToady.setOnClickListener(this);
        fastYes.setOnClickListener(this);
        fastThisWeek.setOnClickListener(this);
        fastThisMonth.setOnClickListener(this);
        fastLastMonth.setOnClickListener(this);


        startTime = view.findViewById(R.id.choose_conditions_start);
        endTime = view.findViewById(R.id.choose_conditions_end);
        TextView commit = view.findViewById(R.id.commit_sure);
        startTime.setOnClickListener(this);
        endTime.setOnClickListener(this);
        commit.setOnClickListener(this);


    }

    /**
     * 获取单例哦
     *
     * @return orderStatusFragment
     */
    public static TimeFragment getInstance() {
        if (timeFragment == null) {
            timeFragment = new TimeFragment();
        }
        return timeFragment;
    }

    private TypeNavigator navigator;

    public void setNavigator(TypeNavigator navigator) {
        this.navigator = navigator;
    }

    /**
     * 清除选中状态
     */
    private void clearChoose() {
        fastAll.setSelected(false);
        fastToady.setSelected(false);
        fastYes.setSelected(false);
        fastThisWeek.setSelected(false);
        fastThisMonth.setSelected(false);
        fastLastMonth.setSelected(false);
    }

    @Override
    public void onClick(View v) {
        clearChoose();

        switch (v.getId()) {
            //不限
            case R.id.fast_choose_all:
                fastAll.setSelected(!fastAll.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("不限");

                break;
            //今天
            case R.id.fast_choose_today:
                fastToady.setSelected(!fastToady.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("今天");
                break;
            //昨天
            case R.id.fast_choose_yesterday:
                fastYes.setSelected(!fastYes.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("昨天");//之前因为是现在换成fragment了
                break;
            //这周
            case R.id.fast_choose_this_week:
                fastThisWeek.setSelected(!fastThisWeek.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("这周");
                break;
            //本月
            case R.id.fast_choose_this_month:
                fastThisMonth.setSelected(!fastThisMonth.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("本月");
                break;
            //上月
            case R.id.fast_choose_last_month:
                fastLastMonth.setSelected(!fastLastMonth.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("上月");
                break;
            //选择开始时间
            case R.id.choose_conditions_start:
                chooseTime(R.id.choose_conditions_start);

                break;
            //结束时间
            case R.id.choose_conditions_end:
                chooseTime(R.id.choose_conditions_end);

                break;
            //提交自定义时间确认
            case R.id.commit_sure:
                //将开始时间和结束时间用减号拼接
                if (TextUtils.isEmpty(startTime.getText().toString()) || TextUtils.isEmpty(endTime.getText().toString())) {
                    Toast.makeText(getContext(), "请选择完整的时间段", Toast.LENGTH_SHORT).show();
                    return;
                }
                navigator.getStartTime(startTimeStr);
                navigator.getEndTime(endTimeStr);
                break;
        }
    }

    /**
     * 选择快速选择时给自定义时间选择置空
     */
    private void setTimeChooseEmpty() {
        startTime.setText("");
        endTime.setText("");
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        format.applyPattern("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 选择时间
     *
     * @param id 控件id
     */
    private void chooseTime(final int id) {

        Log.d(TAG, "chooseTime: 选择时间:");
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                switch (id) {
                    case R.id.choose_conditions_start:
                        startTime.setText(getTime(date));
                        startTimeStr = getTime(date);
                        break;
                    case R.id.choose_conditions_end:
                        endTime.setText(getTime(date));
                        endTimeStr = getTime(date);
                        break;
                }


            }
        })
                .setType(new boolean[]{true, true, true, true, true, true})
                .isDialog(true)
                .build();


        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
            mDialog.show();
        }
    }
}




